<?php
/**
 * Created by IntelliJ IDEA.
 * User: Lukáš
 * Date: 20.12.11
 * Time: 22:06
 * To change this template use File | Settings | File Templates.
 */

class Admin_TitulController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $titul = new Knihovna_Model_DbTable_Titul();
        $this->view->titul = $titul->FetchAll();
        $flashMessage = $this->_helper->flashMessenger;
        $this->view->message = $flashMessage->getMessages();
    }

    public function addAction()
    {
        $form = new Admin_Form_Titul();
        $form->submit->setLabel('Přidej');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();

            if ($form->isValid($formData)) {
                $nazev = $form->getValue('nazev');
                $predjmenem = $form->getValue('predjmenem');

                $titul = new Knihovna_Model_DbTable_Titul();
                $titul->pridejTitul($nazev,$predjmenem);
                $this->_helper->flashMessenger('success|Titul byl úspěšně přidán.');
                $this->_helper->redirector('index');

            } else {
                $form->populate($formData);
            }
        }
    }

    public function editAction()
    {
        $form = new Admin_Form_Titul();
        $form->submit->setLabel('Uložit');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();

            if ($form->isValid($formData)) {

                /*$id = (int)$form->getValue('id_titulu');
                $nazev = $form->getValue('nazev');
                $predjmenem = $form->getValue('predjmenem');*/
                $titul = new Knihovna_Model_DbTable_Titul();
                $post = $form->getValues();
                $data = array(
                    'nazev' => $post['nazev'],
                    'predjmenem' => $post['predjmenem']
                );

                $where = $titul->getAdapter()->quoteInto('id_titulu = ?', $this->_getParam('id_titulu'));
                $titul->update($data,$where);
                //$titul->upravTitul($id, $nazev, $predjmenem);
                $this->_helper->flashMessenger('success|Titul byl úspěšně editován.');
                $this->_helper->redirector('index');

            } else {
                $form->populate($formData);
            }

        } else {
            $id = $this->_getParam('id_titulu', 0);
            if ($id > 0) {
                $titul = new Knihovna_Model_DbTable_Titul();
                $form->populate($titul->najdiTitul($id));
            }
        }
    }

    public function deleteAction()
    {
        $form = new Admin_Form_DeleteTitul();

        if ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('yes', false))) {
            try {
                $id = $this->_getParam('id_titulu');
                $titul = new Knihovna_Model_DbTable_Titul();
                $titul->smazTitul($id);
                $this->_helper->flashMessenger('success|Titul byl úspěšně smazán.');
                $this->_helper->redirector('index');
            } catch (Zend_Db_Exception $error) {
                $this->_helper->flashMessenger('error|' . $error->getMessage());
            }
        } elseif ($this->getRequest()->isPost() && (false !== $this->getRequest()->getPost('no', false))) {
            $this->_redirect('/admin/titul/');
        }

        $this->view->form = $form;
    }

}






